
package com.jy.appmarket.screenlock;

import com.jy.appmarket.R;

import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;

public class LockSoundManager {

	public static int SOUND_ID_LOCK = 1;

	public static int SOUND_ID_UNLOCK = 2;

	public static int SOUND_ID_SHOT = 3;

	private static SoundPool sPool = null;

	/**
	 * 初始化SoundPool
	 */
	public static void initSoundPool(Context context) {
		if (sPool == null) {
			sPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
			SOUND_ID_LOCK = load(context, R.raw.lock);
			SOUND_ID_UNLOCK = load(context, R.raw.unlock);
			SOUND_ID_SHOT = load(context, R.raw.screen_shot);
		}
	}

	/**
	 * 播放对应音频文件
	 * 
	 * @param soundId
	 *            SOUND_ID_LOCK or SOUND_ID_UNLOCK
	 */
	public static void play(Context context, int soundId) {
		if (sPool != null) {
			sPool.play(soundId, 1, 1, 0, 0, 1);
		} else {
			initSoundPool(context);
		}
	}

	/**
	 * 加载音频文件
	 */
	private static int load(Context context, int resId) {
		if (sPool != null) {
			return sPool.load(context, resId, 1);
		} else {
			return 1;
		}
	}

	/**
	 * 释放音频文件
	 */
	private static void unLoad(int soundId) {
		if (sPool != null) {
			sPool.unload(soundId);
		}
	}

	/**
	 * 释放音频文件
	 */
	public static void release() {
		unLoad(SOUND_ID_LOCK);
		unLoad(SOUND_ID_UNLOCK);
		unLoad(SOUND_ID_UNLOCK);
		if (sPool != null) {
			sPool.release();
			sPool = null;
		}
	}
}
